<?php

/**
 * @desc admin page
 * @version $Id: 20120415
 * @author n0aX
 * @package Administration
 */

$item_table = CONFIG_TABLE;

/**
 * @ignore createtime,status
 */

$item_fields = "config_name,config_value";

/**
 * @desc item fields override
 */

$item_override = "";

$where_condition = "1";
$order_by = '';
$settings = '';

switch ($base_request)
{
    case 'display_settings' :
        $item_override = "";
        $config_group = 'display';
        $order_by = "";
        $where_condition = "config_group = '$config_group'";
    break;
    
    case 'rets_settings' :
        $item_override = "";
        $config_group = 'rets';
        $order_by = "";
        $where_condition = "config_group = '$config_group'";
    // @todo
    break;
    
    case 'configure_variables' :
        $item_override = "";
        $config_group = 'variables';
        $order_by = "";
        $where_condition = "config_group = '$config_group'";
    
    break;
    
    case 'change_theme' :
        $item_override = "";
        $config_group = 'theme';
        $order_by = "";
        $where_condition = "config_group = '$config_group' AND config_name='default_template'";
    break;
    
    case 'customize_menus' :
    // @todo
    break;
    
    case 'admin_testimonials' :
        $item_fields = "id,createtime,status,t_name,t_company,t_website,t_city,t_state,t_content,t_images";
        $list_fields = "t_name,t_company,t_state,t_city";
        
        $item_table = TESTIMONIALS_TABLE;
        $item_override = "";
        $config_group = "";
        $order_by = "";
        
        $settings = array(
                'tabs' => array(
                        'new', 
                        'existing'
                ), 
                'form_fields' => array(
                        'createtime' => array(
                                'type' => 'date', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'datepicker required'
                        ), 
                        't_name' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required'
                        ), 
                        't_company' => array(
                                'type' => 'text', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => ''
                        ), 
                        't_website' => array(
                                'type' => 'text', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => ''
                        ), 
                        't_state' => array(
                                'type' => 'select', 
                                'special' => 'states', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required chzn-select', 
                                'default' => ''
                        ), 
                        't_city' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required'
                        ), 
                        't_content' => array(
                                'type' => 'wysiwyg', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'wysiwyg'
                        )
                )
        );
    
    break;
    
    case 'form_fields' :
        // @todo
        $item_fields = "id,createtime,status,ff_name,ff_tag,ff_type,ff_params,ff_required";
        $list_fields = "ff_name,ff_tag,ff_type";
        $item_table = FORMFIELDS_TABLE;
        $item_override = "";
        $config_group = "";
        $order_by = "";
        
        $settings = array(
                'tabs' => array(
                        'new', 
                        'existing'
                ), 
                'form_fields' => array(
                        'ff_name' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required generate-value onkeyup', 
                                
                                'data' => array(
                                        'related' => 'ff_tag', 
                                        'relatedalt' => 'ff_required'
                                )
                        ), 
                        'ff_tag' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required check', 
                                'special' => 'check', 
                                'data' => array(
                                        'name' => 'ff_tag'
                                )
                        ), 
                        'ff_type' => array(
                                'type' => 'select', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required toggler chzn-select', 
                                'values' => $lang[$base_request]['select'], 
                                'data' => array(
                                        'related' => 'ff_params'
                                ), 
                                'default' => 0
                        ), 
                        'ff_params' => array(
                                'type' => 'text', 
                                'required' => 0, 
                                'columns' => 3, 
                                'classes' => '', 
                                'collapsed' => 1
                        ), 
                        'ff_required' => array(
                                'type' => 'checkbox', 
                                'text' => $lang['yes'], 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => 'generate-value onclick', 
                                'value' => 1, 
                                'data' => array(
                                        'related' => 'ff_tag', 
                                        'relatedalt' => 'ff_name'
                                ), 
                                'default' => 0
                        )
                )
        );
    
    break;
    
    case 'custom_forms' :
        $item_fields = "id,createtime,status,f_name,f_tag,f_fields,f_submitvalue,f_captcha,f_redirect,f_notification,f_deletelink";
        $list_fields = "f_name,f_tag";
        $item_table = FORMS_TABLE;
        $item_override = "";
        $config_group = "";
        $order_by = "";
        
        /**
         * @todo multilanguage for f_fields
         */
        
        $settings = array(
                'tabs' => array(
                        'new', 
                        'existing'
                ), 
                'form_fields' => array(
                        
                        'f_name' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required generate-value onkeyup', 
                                
                                'data' => array(
                                        'related' => 'f_tag'
                                )
                        ), 
                        'f_tag' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required check', 
                                'special' => 'check', 
                                'data' => array(
                                        'name' => 'f_tag'
                                )
                        ), 
                        'f_submitvalue' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required'
                        ), 
                        
                        'f_captcha' => array(
                                'type' => 'checkbox', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => '', 
                                'text' => $lang['yes'], 
                                'value' => 1, 
                                'default' => 1
                        ), 
                        
                        'f_redirect' => array(
                                'type' => 'text', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => ''
                        ), 
                        
                        'f_notification' => array(
                                'type' => 'checkbox', 
                                'text' => $lang['yes'], 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => '', 
                                'value' => 1, 
                                'default' => 0
                        ), 
                        
                        'f_deletelink' => array(
                                'type' => 'checkbox', 
                                'text' => $lang['yes'], 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => '', 
                                'value' => 1, 
                                'default' => 0
                        ), 
                        
                        'f_fields' => array(
                                'type' => 'special', 
                                'columns' => 1
                        )
                )
        );
    
    break;
    
    case 'custom_pages' :
        $item_fields = "id,status,page_title,page_desc,page_keywords,content,page_group,page_name,page_url,exclude_template,
        exclude_title,navigation_menu,page_squeeze,squeeze_page,squeeze_form";
        $list_fields = "page_name,page_url,page_title";
        $item_table = CONTENT_TABLE;
        $item_override = "";
        $config_group = "";
        $order_by = "";
        
        $settings = array(
                'tabs' => array(
                        'new', 
                        'existing'
                ), 
                'form_fields' => array(
                        
                        'page_name' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 3, 
                                'classes' => 'required'
                        ), 
                        'page_url' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 3, 
                                'classes' => 'required check', 
                                'special' => 'check', 
                                'data' => array(
                                        'name' => 'page_url'
                                )
                        ), 
                        
                        'page_title' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 3, 
                                'classes' => 'required'
                        ), 
                        
                        'page_desc' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required'
                        ), 
                        
                        'page_keywords' => array(
                                'type' => 'text', 
                                'required' => 1, 
                                'columns' => 2, 
                                'classes' => 'required'
                        ), 
                        
                        'page_group' => array(
                                'type' => 'select', 
                                'required' => 1, 
                                'columns' => 3, 
                                'classes' => 'required chzn-select', 
                                'values' => $lang[$base_request]['select'], 
                                'default' => '0'
                        ), 
                        
                        'navigation_menu' => array(
                                'type' => 'radio', 
                                'required' => 0, 
                                'columns' => 3, 
                                'classes' => '', 
                                'values' => array(
                                        '1' => $lang['yes'], 
                                        '0' => $lang['no']
                                ), 
                                'default' => 1
                        ), 
                        
                        'exclude_template' => array(
                                'type' => 'radio', 
                                'required' => 0, 
                                'columns' => 3, 
                                'classes' => '', 
                                'values' => array(
                                        '1' => $lang['yes'], 
                                        '0' => $lang['no']
                                ), 
                                'default' => 0
                        ), 
                        
                        'page_squeeze' => array(
                                'type' => 'checkbox', 
                                'text' => $lang['yes'], 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => 'trigger-toggle onchecked', 
                                'value' => 1, 
                                'data' => array(
                                        'related' => 'squeeze_form', 
                                        'relatedalt' => 'squeeze_page_list'
                                ), 
                                'default' => 0
                        ), 
                        
                        'squeeze_form' => array(
                                'type' => 'select', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => 'chzn-select', 
                                'collapsed' => 1, 
                                'default' => 0
                        ), 
                        
                        'squeeze_page' => array(
                                'type' => 'special', 
                                'required' => 0, 
                                'collapsed' => 1, 
                                'columns' => 2
                        ), 
                        
                        'content' => array(
                                'type' => 'wysiwyg', 
                                'required' => 0, 
                                'columns' => 2, 
                                'classes' => 'wysiwyg'
                        )
                )
        );
    
    break;
    
    case 'website_info' :
    default :
        $item_override = "";
        $config_group = 'info';
        $where_condition = "config_group = '$config_group'";
        $order_by = "";
        $upload_fields = array(
                'logo', 
                'logo_portal'
        );
    
    break;
}

if (is_array($settings))
{
    $settings['buttons'] = $lang[$base_request]['buttons'];
    if ($list_fields != '')
    {
        $listing_js = preg_replace('%([^,]+)%i', '{"mDataProp":"$1"}', $list_fields) . ',';
        $settings['listing'] = explode(',', $list_fields);
        $settings['listing_js'] = $listing_js;
    }
    $tk->page->_pv['settings'] = $settings;
}

if ($tk->page->request == 'json')
{
    include (__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'json_errors.php');
    
    if (is_file(__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'ajax' . DS . 'ajax_' . $tk->page->_pv['admin_page'] . '.php'))
    {
        include (__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'ajax' . DS . 'ajax_' . $tk->page->_pv['admin_page'] . '.php');
    }
}

if ($item_table == CONFIG_TABLE)
{
    $sql = "SELECT SQL_CACHE $item_override $item_fields from $item_table WHERE $where_condition $order_by";
    
    if (! ($result = $tk->db->sql_query($sql)))
    {
        $error = 'sql';
        // @todo fix: function message_die($request,$message,$line,$file,$details = '',$code = -1,$header = '',$status = 'error') {
        $tk->eh->message_die($error, __LINE__, __FILE__, $sql);
        return;
    }
    
    while($row = $tk->db->sql_fetchrow($result))
    {
        $data[$row['config_name']] = $row['config_value'];
    }
}

if ($item_table == FORMS_TABLE)
{
    $sql = "SELECT SQL_CACHE ff_name,ff_tag,ff_type,ff_params,ff_required,id from " . FORMFIELDS_TABLE . " WHERE status = '1' order by ff_name asc";
    
    if (! ($result = $tk->db->sql_query($sql)))
    {
        $error = 'sql';
        // @todo fix: function message_die($request,$message,$line,$file,$details = '',$code = -1,$header = '',$status = 'error') {
        $tk->eh->message_die($error, __LINE__, __FILE__, $sql);
        return;
    }
    
    while($row = $tk->db->sql_fetchrow($result))
    {
        $data['ffields'][$row['ff_type']][$row['ff_tag']] = $row;
    }
}

if ($item_table == CONTENT_TABLE)
{
    $sql = "SELECT SQL_CACHE f_name,f_tag,id from " . FORMS_TABLE . " WHERE status = '1' order by f_name asc";
    
    if (! ($result = $tk->db->sql_query($sql)))
    {
        $error = 'sql';
        // @todo fix: function message_die($request,$message,$line,$file,$details = '',$code = -1,$header = '',$status = 'error') {
        $tk->eh->message_die($error, __LINE__, __FILE__, $sql);
        return;
    }
    
    while($row = $tk->db->sql_fetchrow($result))
    {
        $data['custom_forms'][$row['id']] = $row;
    }
}

$tk->page->_pv['list_fields'] = explode(',', $list_fields); 

